﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows;
using System.Windows.Media.Animation;
using System.Windows.Media;
    
namespace TouchControls
{
    public partial class TouchPivot
    {
        private const double _scrollAccelerator = 2;
        //ManipulationHook _hook = new ManipulationHook();
        ManipulationTracker _tracker;

        private Point ptOrigine;

        protected override void OnManipulationStarted(ManipulationStartedEventArgs e)
        {
            _tracker = new ManipulationTracker(Orientation.Horizontal);

            ptOrigine = (e.ManipulationContainer as UIElement).TransformToAncestor(this).Transform(e.ManipulationOrigin);
            
            if (Items.Count > 1)
            {
                _tracker.StartTracking(new Point(0, 0)); //??
            }
        }

        protected override void OnManipulationDelta(ManipulationDeltaEventArgs e)
        {            
            var ptCurrent = (e.ManipulationContainer as UIElement).TransformToAncestor(this).Transform(e.ManipulationOrigin);
            var cumul = new Point(ptCurrent.X - ptOrigine.X , ptCurrent.Y - ptOrigine.Y );

            // manipulation canceled
            if (_tracker.Canceled)
            {
                return;
            }

            if (_tracker.TrackManipulation(cumul))
            {
                e.Handled = true;

                if (Math.Abs(cumul.X * _scrollAccelerator) >= ActualWidth)
                {
                    if (cumul.X > 0)
                    {
                        _itemsTranslate.X = ActualWidth;
                        _headersTranslate.X = _ghostHeader.ActualWidth;
                    }
                    else
                    {
                        _itemsTranslate.X = - ActualWidth;
                        var item = (PivotItem)Items[SelectedIndex];
                        _headersTranslate.X = - item.Header.ActualWidth;
                    }                    
                    
                    //_tracker.CompleteTracking();
                }
                else
                {
                    _itemsTranslate.X = (cumul.X * _scrollAccelerator);
                    if (cumul.X > 0)
                    {
                        var item = this.GetPrevItem();
                        _headersTranslate.X = cumul.X * _scrollAccelerator * (_ghostHeader.ActualWidth) / ActualWidth;
                    }
                    else
                    {
                        var item = (PivotItem)Items[SelectedIndex];
                        _headersTranslate.X = cumul.X * _scrollAccelerator * (item.Header.ActualWidth - _firstMenuMargin) / ActualWidth;
                    }
                }
            }
            
            if (_tracker.Completed) e.Complete();
        }

        protected override void OnManipulationCompleted(ManipulationCompletedEventArgs e)
        {
            var ptCurrent = (e.ManipulationContainer as UIElement).TransformToAncestor(this).Transform(e.ManipulationOrigin);
            var cumul = new Point(ptCurrent.X - ptOrigine.X, ptCurrent.Y - ptOrigine.Y);
            // manipulation canceled
            if (_tracker.Canceled) return;
            double direction = 0;
            
            if (null != e.FinalVelocities)
            {
                direction = (int)Math.Sign(-e.FinalVelocities.LinearVelocity.X);
            }

            if (Math.Abs(cumul.X * _scrollAccelerator) > ActualWidth / 2)
            {
                direction = cumul.X / Math.Abs(cumul.X);

                // move previous/next
                if (direction < 0)
                {
                    ScrollNext();
                    return;
                }
                else
                {
                    ScrollPrev();
                    return;
                }
            }

            this.CancelTranslationAnimation();

            // not moving at all ?
            // let's find out if a header was clicked
            if (_tracker.Delta.X == 0)
            {
                var elt = VisualTreeHelper.HitTest(this, e.ManipulationOrigin);
                if (elt != null && elt.VisualHit is UIElement)
                {
                    var header = (elt.VisualHit as UIElement).FindParent<PivotHeader>();
                    if (header != null)
                    {
                        e.Handled = true;
                        int index = Items.IndexOf(header.Item);
                        if (index >= 0 && index != SelectedIndex)
                        {
                            ScrollTo(index);                            
                        }
                        return;
                    }
                }
            }
            e.Cancel();
        }
    }
}
